
#include "main.h"

bool gb_WaterProcess;           		//进水处理
bool gb_TemptrDown;             		//进水温度下降
bool gb_DryFireFlag;
u8 gu8_AlarmSoundCnt;					//警报
u8 gu8_AddWaterTime1;            		//进水时间
u8 gu8_AddWaterTime2;             		//进水时间
u8 gu8_AddWaterDly;             		//延时干烧判断
u8 gu8_AddWatterheatdelay;
u8 gu8_AddWatterdelay;
u8 gu8_AddWaterCnt;
u8 gu8_FAdWdelay;
u8 look_setp1;
u16 look_temp;
u8 gu8_draintime;
/***************************************************************************************
* Function Name  : 
* Description    : 
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void CheckAddWaterone(void)
{
    static u8 gu8_DnDly = 0;
//    static u8 ct = 0;
    static u8 gu8_TemptrAtAdd = 0;
    /* 为区别是开始时加水，还是工作中缺水加水。*/
    static bool gb_AddFlag = false;
    s8  gs8_DltTemptr;
	
	/*******新方法变量*********/
	static 	float 	temper_sum = 0;						//1秒内10个底盘温度值的和
	static 	u8 		temper_num = 0;						//底盘温度值个数计数
	static	float 	temper_buff[5]={0,0,0,0,0};			//每秒产生的底盘温度值	
	
	static 	float 	temper_up_arg[5] = {0,0,0,0,0};
	static 	float 	temper_up_temp = 0;				//温度下降加水，5秒内5个底盘温度值的和
	static 	u8 		temper_up_num = 0;				//底盘温度值个数计数
	static  u8 		Stable_num = 0;						//稳态判断时间
//			u8 		up = 0;
	
    // static u8  		btm_temptr_arr[60];
	// static u8  		btm_temptr_min[5];
    // static bool 	gb_ArrReadyFlag = 0;        //数组
    // static u8 		gu8_ArrCt = 0;
	// static bool 	gb_arrive = 0;
	// static u8 	    gu8_sct = 0;
	// static u8 	    gu8_avgtemp = 0;
	// static u16      gu16_avgdelay = 0;
    // static u8       gu8_offcnt = 0;
//    u8  i;
//    u16 eptemp = 0;	
	if(system_sec_f)
	{
		if((gu8_AddWatterdelay > 0)&&(gu8_AddWaterTime1 == 0))
		{
			gu8_AddWatterdelay--;
		}
	}
    if(steamCtrl.state == STA_WORK)
    {
        if(gb_WaterProcess)
        {
//            look_setp1 = 1;
			if(gb_DryFireFlag)
			{		
				Stable_num = 0;
				temper_up_num = 0;
				temper_up_temp = 0;				
				return;
			}
            	
			/*******新方法变量*********/
			if(gu8_AddWaterTime2 ||gu8_AddWaterTime1)
			{			
				Stable_num = 0;
				temper_up_num = 0;
				temper_up_temp = 0;					
				gu8_DnDly = 40;				
			}
			if((ht_Ibom_out.perc == 0)&&(ht_Itop_out.perc == 0))
			{
				Stable_num = 0;
				temper_up_num = 0;
				temper_up_temp = 0;
                    			
			}

			temper_sum += tempr_bom.cur;
			temper_num++;	
			if(temper_num >= 10) //周期1s
			{ 
				temper_sum /= temper_num;//10个100ms的平均值
				temper_buff[0] = temper_buff[1];
				temper_buff[1] = temper_buff[2];
				temper_buff[2] = temper_buff[3];
				temper_buff[3] = temper_buff[4];  
				temper_buff[4] = temper_sum;   //数据更新
				temper_sum = 0;
				temper_num = 0;
				
			}				
			/****************/  
            
            if(gb_AddFlag)
            {//加水过程
                /*  
                    加水后温度很快下降，小于5秒时温度已经稳定，有可能判断不到。
                    加水后全功率加热有出现连续两次加水，用add_water_dly延时。
                    加水后，温度还会上冲，3分之1水量时，到最高点下降时间只有3、4秒。
                */  
                if ((gu8_AddWaterTime1) && (tempr_bom.cur > gu8_TemptrAtAdd))     //加水过程中最高温度，也有可能温度不下降
                    gu8_TemptrAtAdd = tempr_bom.cur; 
				gs8_DltTemptr = gu8_TemptrAtAdd - tempr_bom.cur;	//判断加水时侯温度有没有下降 
                if ((gs8_DltTemptr >= 10) && (tempr_bom.cur < BOIL_TEMPTR_H0))//温度下降10度并且温度小于110度时
                {//提前恢复加热
                    gb_TemptrDown = true;
                    gb_AddFlag = false;
					look_setp1 = 0;
                    gu8_DnDly  = ADD_WATER_DELAY;	  //延时干烧的判断
                }            
                /* 不用 if (add_water_tmr || add_water_dly)时，不能用(!add_water_tmr)判断，因为不会进入了!!! */
                //if ((!add_water_tmr) && ((dlt_temptr >= 0) || (bottom_temptr >= BTM_ERR_TEMPTR)))     //水较少，预热阶段，上冲到128，加水，有误报缺水。
				// if((!gu8_AddWaterTime1)&& (gu8_AddWaterCnt > 3)&&(tempr_bom.cur > 120))//3
                // {//缺水判断，加了水又没输出，温度应该下降！！

				// 	gb_DryFireFlag = 1;
				// 	gu8_FAdWdelay = 0;						
				// 	gu8_AddWatterdelay = 0;							
				// 	gb_AddFlag = false;					
				// 	look_setp1 = 0;
                // }
                if(!gu8_AddWaterTime1) //加水时间到结束
                {
                    gb_AddFlag = false;          //时间到结束，认为加水成功
					gb_TemptrDown = 0;
                    gu8_DnDly  = 40;     //延时干烧的判断
					if(tempr_bom.cur < 120)
						gu8_AddWaterCnt = 0;					
                }                
            }
            else
            {
				if(gu8_AddWatterdelay > 0)//加完水延迟50秒判断
					return;							
				if (gu8_AddWaterTime1 || gu8_AddWaterTime2)  return;		//进水时返回
				if((gu8_AddWaterCnt > 3)&&(tempr_bom.cur >= BOIL_TEMPTR_H))//3
                {//缺水判断，加了水又没输出，温度应该下降！！

					gb_DryFireFlag = 1;
					gu8_FAdWdelay = 0;						
					gu8_AddWatterdelay = 0;							
					gb_AddFlag = false;					
					gu8_AddWaterCnt = 0;
					return;
                }				
				if(tempr_bom.cur < BOIL_TEMPTR_H)	
				{
					gu8_AddWaterCnt = 0;
				}							
                if (tempr_bom.cur >= BOIL_TEMPTR_H)  //115度   
                {					
                    goto  add_water_jmp; 
                }
	
                gu8_TemptrAtAdd = 0;				//加水时底盘温度变化记录
				if(system_sec_f)
					u8_dn(&gu8_DnDly);
				if (gu8_DnDly) return;
				if( temper_num == 0)
				{
					{
						if((temper_buff[4] >= 100))//上升曲线//||gb_ArrReadyFlag
						{
							temper_up_num++;
							temper_up_temp += temper_buff[4];
							if(temper_up_num >= 1) //3个1s的平均数 
							{
								temper_up_temp /= 1;
								temper_up_arg[0] = temper_up_arg[1];
								temper_up_arg[1] = temper_up_arg[2];
								temper_up_arg[2] = temper_up_arg[3];
								temper_up_arg[3] = temper_up_arg[4];
								temper_up_arg[4] = temper_up_temp;
//								look_temp = temper_up_temp;
								temper_up_temp = 0;
								temper_up_num = 0;
																
						/***********小功率管工作温度上升2度*****************/
								if(temper_up_arg[0] < (temper_up_arg[1])) //检测到上升曲线，5个1秒连续上升，上升大于0.3度
								{
									
									if(temper_up_arg[1] < (temper_up_arg[2]))//上升大于0.3度
									{
				
										if(temper_up_arg[2] < (temper_up_arg[3]))
										{
											
											if(temper_up_arg[3] < (temper_up_arg[4]))
											{
												
//												up = temper_up_arg[4] - temper_up_arg[0];
												if(temper_up_arg[4] - temper_up_arg[0] >= 4)
												{
													
													if((temper_up_arg[4] >= 115)&& (Stable_num > 120)&&(steamPara.menu != WORK_CHUJUN)) //曲线有波动，认为稳定了两秒才进入,加水90秒后，温度稳定了2秒，
													{
														
														Stable_num = 0;
														//goto  add_water_jmp;
														add_water_jmp:
															if(steamPara.menu == WORK_CHUJUN)
															{
																gu8_TemptrAtAdd = tempr_bom.cur;
																if(gu8_DisinfWorkStatus == 1)
																{
																	gu8_AddWaterTime1 = 45;	//进水时间	
																	gu8_AddWatterdelay = 60;
																	gu8_AddWaterCnt++;
																	gb_AddFlag = true;	//加水																	
																}
																else
																{
																	gb_WaterProcess = 0;
																	gb_AddFlag = false;	//加水												
																}
															}
															else
															{

																gu8_AddWaterTime1 = 45;	//进水时间
																gu8_AddWatterdelay = 60;
																gu8_AddWaterCnt++;
																gb_AddFlag = true;	//加水																
																gu8_TemptrAtAdd = tempr_bom.cur; 		
																				
															}															
													}
												}
											}
											else
											{
												Stable_num++; //曲线有波动，认为稳定了
											}
										}
										else
										{
											Stable_num++; //曲线有波动，认为稳定了
										}
									}
									else
									{
										Stable_num++; //曲线有波动，认为稳定了
									}
								}
								else
								{
									Stable_num++; //曲线有波动，认为稳定了
								}							
							}
							Stable_num = (Stable_num >= 250) ? 250 : Stable_num; //max = 200;
						}
						else
						{
							Stable_num = 0;//稳定过低，清除波动
							temper_up_arg[0] = temper_buff[4];
							temper_up_arg[1] = temper_buff[4];
							temper_up_arg[2] = temper_buff[4];
							temper_up_arg[3] = temper_buff[4];
							temper_up_arg[4] = temper_buff[4];
							
							temper_up_num = 0;
							temper_up_temp = 0;	
//							look_temp = temper_up_temp;							
						}
					}
				}									              
            }
        }
        else
            goto clean_jmp;
    }
    else
    {

    clean_jmp:     
        gb_AddFlag = false;
        gu8_AddWatterheatdelay = 0;
		gu8_AddWaterCnt = 0;
		Stable_num = 0;
		temper_num = 0;
		temper_up_arg[0] = tempr_bom.cur;
		temper_up_arg[1] = tempr_bom.cur;
		temper_up_arg[2] = tempr_bom.cur;
		temper_up_arg[3] = tempr_bom.cur;
		temper_up_arg[4] = tempr_bom.cur;	
        if((steamCtrl.state != STA_PAUSE))
        {
            gu8_AddWaterTime2 = 0;
            gu8_AddWaterTime1 = 0;
        }		       
    }
}
/***************************************************************************************
* Function Name  : 
* Description    : 蒸模式加水判断
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void CheckAddWater2(void)
{
	static u16 gu16_sigct = 0;
	if(steamCtrl.state == STA_WORK)
    {
		if(gu8_AddWaterTime1 || gu8_AddWaterTime2)
		{
			gu16_sigct = 0;
		}			
		if(!system_sec_f)
			return;
        if(gb_WaterProcess)
        {
			
 			if((tempr_bom.cur >= 95)&&(steamPara.tempr_set >= 60))//底盘温度大于100度ht_Ibom_out.perc == 0 && ht_Obom_out.perc == 0
			{
				//gu16_sigct = 0;
				if ((ht_Ibom_out.perc) ||(ht_Itop_out.perc)) // 30功率时加一次水可工作30分钟以上。
				{//100度纯蒸，水箱有一半水时(200*40)，升温阶段就不会缺水加水。    
                    if(ht_Ibom_out.perc)
                    {
                        gu16_sigct += (100 *ht_Ibom_out.perc)/100;
                    }
					if(ht_Itop_out.perc)
					{
						gu16_sigct += (100 *ht_Itop_out.perc)/100;
					}
					if((steamPara.menu != WORK_CHUJUN)&&(steamPara.menu != WORK_DESCAL)&&(steamPara.menu != WORK_SOFT))
					{
						if(gb_skeeptemper == 0)
						{
							if(gu16_sigct >= 13000)
							{								
								gu8_AddWaterTime2 = ADD_WATER_TIME2;	
								if(gu8_AddWatterdelay < 45)
									gu8_AddWatterdelay = 45;							
								gu16_sigct = 0;
							}
						}
						else 
						{
							if(steamPara.tempr_set >= 95)
							{
								if(gu16_sigct >= 14000)
								{								
									gu8_AddWaterTime2 = ADD_WATER_TIME2;
									if(gu8_AddWatterdelay < 45)
										gu8_AddWatterdelay = 45;									
									gu16_sigct = 0;
								}								
							}
							else if(steamPara.tempr_set >= 80)
							{
								if(gu16_sigct >= 16000)
								{								
									gu8_AddWaterTime2 = ADD_WATER_TIME2;
									if(gu8_AddWatterdelay < 45)
										gu8_AddWatterdelay = 45;								
									gu16_sigct = 0;
								}	
							}
							else
							{
								if(gu16_sigct >= 16000)
								{								
									gu8_AddWaterTime2 = 15;
									if(gu8_AddWatterdelay < 45)
										gu8_AddWatterdelay = 45;							
									gu16_sigct = 0;
								}									
							}
						}													
					}
					else
					{
						gu16_sigct = 0;
					}
				}
			} 
			else 
				gu16_sigct = 0;
		}
	}
	else
	{
        if((steamCtrl.state != STA_PAUSE))
        {
            gu8_AddWaterTime2 = 0;
            gu8_AddWaterTime1 = 0;
            gu16_sigct = 0;
        }
	}
}
